package mattypserver.dyndns.org.oah.impl;

import mattypserver.dyndns.org.oah.OAuthHelper;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class BasicOAuthHelperActivity extends Activity {

	public static final String TAG = "BasicOAuthHelperActivity";
	private static OAuthHelper oAuthHelper;
	private Intent rtIntent;
	private SharedPreferences pref;
	private String cKey;
	private String cSecret;
	private String rTokenUrl;
	private String acTokenUrl;
	private String auTokenUrl;
	private String callBackUrl;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupOAuthHelper();
		kickOffOAuthFlow();
		Log.d(TAG, "Instance OnCreate: [" + this.toString() + "]");
	}

	@Override
	protected void onNewIntent(Intent in){
		super.onNewIntent(in);
		Log.d(TAG, "OnResume Instance : [" + this.toString() + "]");
		if (in.getData() != null) {
			
			Uri uri = in.getData();
			Log.d(TAG, "URI in onResume: [" + uri + "]");

			Log.d(TAG, "in on resume, from oauth call");
			oAuthHelper.retrieveOAuthVerifier(in);
			oAuthHelper.retrieveAccessToken();
			
			Editor edit = pref.edit();
			
			edit.putString(BasicOAuthConstants.AC_TOKEN_K, oAuthHelper
					.getAccessToken().getAccessToken());
			edit.putString(BasicOAuthConstants.AC_SECRET_K, oAuthHelper
					.getAccessToken().getTokenSecret());
			edit.commit();
			
			this.finish();
		}

	}

	public void setupOAuthHelper() {
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {

			cKey = extras.getString(BasicOAuthConstants.C_KEY_K);
			cSecret = extras.getString(BasicOAuthConstants.C_SECRET_K);
			rTokenUrl = extras
					.getString(BasicOAuthConstants.R_TOKEN_URL_K);
			acTokenUrl = extras
					.getString(BasicOAuthConstants.AC_TOKEN_URL_K);
			auTokenUrl = extras
					.getString(BasicOAuthConstants.AU_TOKEN_URL_K);
			callBackUrl = extras
					.getString(BasicOAuthConstants.CALL_BACK_URL_K);
			pref = getSharedPreferences(extras.getString(BasicOAuthConstants.OAUTH_PREFS), MODE_WORLD_WRITEABLE);
			
			oAuthHelper = new BasicOAuthHelper(cKey, cSecret, rTokenUrl,
					acTokenUrl, auTokenUrl, callBackUrl);
			Log.d(TAG, "OAUTH Helper setup");
			
		}

	}
	
	public void kickOffOAuthFlow(){
		oAuthHelper.retrieveRequestToken();

		try {
			String uri = oAuthHelper.getRequestToken().getRequestToken();
			if (uri != null && (auTokenUrl != null && !auTokenUrl.equals(""))){
				Log.d(TAG, uri);
				rtIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
				rtIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
						| Intent.FLAG_ACTIVITY_NO_HISTORY
						| Intent.FLAG_FROM_BACKGROUND);
				startActivity(rtIntent);
			} else {
				Log.d(TAG, "uri is null");
				Log.i(TAG, "Attempting to gain access with requestToken");
				oAuthHelper.retrieveAccessToken();
				
				Editor edit = pref.edit();
				
				edit.putString(BasicOAuthConstants.AC_TOKEN_K, oAuthHelper
						.getAccessToken().getAccessToken());
				edit.putString(BasicOAuthConstants.AC_SECRET_K, oAuthHelper
						.getAccessToken().getTokenSecret());
				edit.commit();
				
				this.finish();
				
			}
		} catch (Exception e) {
			Log.d(TAG, "Exception kicking off OAuthFlow: [" + e.toString() + "]");
		}
	}

	public OAuthHelper getOAuthHelper() {
		return oAuthHelper;
	}

	public void onResumePostOAuth() {

	}
}
